package com.itheima.reggie;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
/*
pom.xml
<dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.6.0</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
            <version>2.2.1</version>
        </dependency>
*/

public class SendSms {

    public static void sendMsg(String phone , String code) {
        /**
         * 密钥key (关联的就是扣款的账户)
         */
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAIkgTcRMp5Nutt", "wfuZPV0r9G1t30FoKp1CgYQPClJYqf");
        /** use STS Token
        DefaultProfile profile = DefaultProfile.getProfile(
            "<your-region-id>",           // The region ID
            "<your-access-key-id>",       // The AccessKey ID of the RAM account
            "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
            "<your-sts-token>");          // STS Token
        **/

        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSignName("品优购"); //签名（申请）
        request.setTemplateCode("SMS_158491775"); //短信模板（申请）
        request.setTemplateParam("{\"code\":\""+code+"\"}"); //验证码（修改）
        request.setPhoneNumbers(phone); //手机号 （修改）
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

    }


    public static void main(String[] args) {
        String code = new Random().nextInt(100000) + "";
        System.out.println(code);
        SendSms.sendMsg("18296373716",code);
    }
}
